package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.constant.Constants;
import cn.itsource.ymcc.constant.PayConstants;
import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.domain.Course;
import cn.itsource.ymcc.domain.CourseSummary;
import cn.itsource.ymcc.domain.CourseUserLearn;
import cn.itsource.ymcc.mapper.CourseMapper;
import cn.itsource.ymcc.mapper.CourseSummaryMapper;
import cn.itsource.ymcc.mapper.CourseUserLearnMapper;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "CourseConsumer",
        topic = PayConstants.TOPIC_PAY_ORDER_MESSAGE,
        selectorExpression = PayConstants.TAGS_PAY_ORDER_MESSAGE
)
public class CourseConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private CourseUserLearnMapper courseUserLearnMapper;
    @Autowired
    private CourseSummaryMapper courseSummaryMapper;
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String data = new String(body, Constants.UTF8);
            JSONObject jsonObject = JSONObject.parseObject(data);
            String courseIds = jsonObject.get("courseIds").toString();
            String[] split = courseIds.split(",");
            List<String> listCourseIds = Arrays.asList(split);
            List<Course> courses = courseMapper.selectBatchIds(listCourseIds);

            //新增购买记录
            for (Course cours : courses) {
                CourseUserLearn courseUserLearn = new CourseUserLearn();
                courseUserLearn.setLoginId(Long.valueOf(jsonObject.get("userId").toString()));
                courseUserLearn.setStartTime(cours.getStartTime());
                courseUserLearn.setEndTime(cours.getEndTime());
                courseUserLearn.setState(RConst.Num.INT_0);
                courseUserLearn.setCourseId(cours.getId());
                courseUserLearn.setCourseOrderNo(jsonObject.get("orderNo").toString());
                courseUserLearn.setCreateTime(new Date());
                courseUserLearnMapper.insert(courseUserLearn);

            }
            //添加购买记录
            List<CourseSummary> courseSummaries = courseSummaryMapper.selectBatchIds(listCourseIds);
            for (CourseSummary courseSummary : courseSummaries) {
                courseSummary.setSaleCount(courseSummary.getSaleCount()+1);
                courseSummaryMapper.updateById(courseSummary);
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
